<template>
  <div class="reg">
    <router-link to="/"
      ><img
        src="https://t7.baidu.com/it/u=1956604245,3662848045&fm=193&f=GIF"
        class="logo"
        alt=""
    /></router-link>
    <van-form @submit="onSubmit">
      <van-field
        v-model="userName"
        name="userName"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <van-field
        v-model="repassword"
        type="password"
        name="repassword"
        label="确认密码"
        placeholder="请再次输入密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >注册</van-button
        >
      </div>
    </van-form>
    <router-link :to="{ name: 'Login' }">已有账号，我要登录</router-link>
  </div>
</template>

<script>
import { Notify } from "vant";
import { regAPI } from "../services/auth";
export default {
  data() {
    return {
      userName: "",
      password: "",
      repassword: "",
    };
  },
  methods: {
    async onSubmit() {
      if (this.password != this.repassword) {
        Notify({ type: "warning", message: "两次密码不一致" });
      }
      const info = await regAPI({
        userName: this.userName,
        password: this.password,
      });
      console.log(info);
      if (info.code == 1) {
        Notify({ type: "success", message: "注册成功" });

        this.$router.replace({
          //成功后跳转登录
          name: "Login",
        });
      } else {
        Notify({ type: "danger", message: "注册异常" });
      }
    },
  },
};
</script>

<style scoped>
.logo {
  width: 45vw;
  display: block;
  border-radius: 20px;
  margin: 30px auto;
}
</style>
